package com.jeehentor.config.security.handler;

import com.alibaba.fastjson2.JSON;
import com.jeehentor.common.vo.Result;
import com.jeehentor.utils.WebUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;


/**
 * @Description : 403配置
 * AccessDeniedHandler 访问一个需要需要认证的资源,身份认证成功(登录成功) 但是角色和权限不足时 该接口会被调用, 用来返回: 重定向到错误页面、返回特定的错误响应等
 * @Author : wzd
 * @Date : 2024/4/9 11:00
 * @Version : 1.0
 **/
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
        Result<String> result = new Result<>();
        result.setCode(HttpStatus.FORBIDDEN.value());
        result.setMessage("无权限访问");
        WebUtil.renderString(response,JSON.toJSONString(result));
    }
}
